package com.usian.controller;

import com.usian.model.dtos.XiansuoDto;
import com.usian.model.pojos.Provinces;
import com.usian.model.pojos.Xiansuo;
import com.usian.model.pojos.XiansuoResult;
import com.usian.service.XiansuoService;
import com.usian.utils.AppHttpCodeEnum;
import com.usian.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/xiansuo")
public class XiansuoController {

    @Autowired
    private XiansuoService xiansuoService;




    @PostMapping("/list")
    public ResponseResult list(@RequestBody XiansuoDto dto){
        return xiansuoService.xiansuoList(dto);
    }

    @GetMapping("/pro")
    public List<Provinces> proList(@RequestParam Integer parentId,@RequestParam Integer rank){
        return xiansuoService.findPro(parentId,rank);
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody Xiansuo xiansuo){
        return xiansuoService.saveXiansuo(xiansuo);
    }

    @GetMapping("/deleteById")
    public ResponseResult deleteById(@RequestParam Integer id){
        Xiansuo xiansuo = xiansuoService.getById(id);
        if(xiansuo.getStatus()==0){
            boolean b = xiansuoService.removeById(id);
            if(b){
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
    }

    @PostMapping("/shenhe")
    public ResponseResult shenhe(@RequestBody Xiansuo xiansuo){
        return xiansuoService.shenhe(xiansuo);
    }


}
